package com.haodai.agency.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.SystemClock;

import com.haodai.agency.application.MyApplication;

import net.bither.util.NativeUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanday on 2017/3/31.
 * 需配合lib-bither-compress使用
 */
public class ImageCompressUtil {
    //压缩后的路径集合
    private static List<String> desLists=new ArrayList<>();
    //压缩的存储位置
    private static String cacheFile= MyApplication.CONTEXT.getCacheDir().getAbsolutePath()+"/";
    private static Context ctx;
    /**
     * 图片压缩封装方法
     * @param sourceLists 带压缩的图片集合
     * @return 压缩后的图片集合
     */
    public static List<String> compressBitmap(final List<String> sourceLists) {
        desLists.clear();

        if(sourceLists.size()==1){
            String desPath=cacheFile+new File(sourceLists.get(0)).getName();
            Logs.e("path is ",sourceLists.get(0));
            while (new File(sourceLists.get(0)).length()==0){
                SystemClock.sleep(10);
            }
            Logs.e("path size ",new File(sourceLists.get(0)).length());
            Bitmap image= BitmapFactory.decodeFile(sourceLists.get(0));
            NativeUtil.compressBitmap(image,desPath);
            desLists.add(desPath);
            return desLists;

        }


        for (String sourcePath : sourceLists) {
            String desPath=cacheFile+new File(sourcePath).getName();
            NativeUtil.compressBitmap(BitmapFactory.decodeFile(sourcePath),desPath);
            Logs.e("====>>>>",new File(desPath).length()/1000);
            desLists.add(desPath);
        }
        return desLists;
    }
}
